package com.yy.bolg.util;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.github.pagehelper.Page;
import com.yy.bolg.json.BaseJson;
import com.yy.bolg.json.CommonlyJson;

import java.util.Map;

/**
 * 返回JSON字符串工具类
 * @author enmaoFu
 * @date 2024-02-29
 */
public class RespenseJsonTransformationUtilNew {

    public static CommonlyJson jsonStr(int code, String msg, Map<String,Object> data){
        String str;
        CommonlyJson responseJson = new CommonlyJson();
        responseJson.setCode(code);
        responseJson.setMesage(msg);
        if(null != data.get("data")){
            responseJson.setData(data.get("data"));
        }else{
            responseJson.setData(data);
        }
        if(null != data.get("pageInfo")){
            Page page = (Page) data.get("pageInfo");
            data.put("pageNum", page.getPageNum());
            data.put("pageSize", page.getPageSize());
            data.put("total", page.getTotal());
            data.put("pagas", page.getPages());
            data.remove("pageInfo");
        }
        str = JSONObject.toJSONString(responseJson,
                SerializerFeature.WriteNullStringAsEmpty,   // 字符类型字段如果为null,输出为“”,而非null
                SerializerFeature.WriteNullNumberAsZero,    // 数字类型字段如果为null,输出为0,而非null
                SerializerFeature.WriteNullListAsEmpty,     // List类型字段如果为null,输出为[],而非null
                SerializerFeature.WriteNullBooleanAsFalse); // Boolean类型字段如果为null,输出为false,而非null
        return responseJson;
    }

}
